package com.ruoyi.system.service.admin;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.RedisKeyConstants;
import com.ruoyi.common.core.domain.entity.admin.AdminXiaoYuanReimbursement;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.global.CustomException;
import com.ruoyi.common.utils.sql.MyPageUtils;
import com.ruoyi.system.mapper.admin.AdminXiaoYuanReimbursementMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AdminXiaoYuanReimbursementService {

    @Resource
    private AdminXiaoYuanReimbursementMapper adminXiaoYuanReimbursementMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询后台-小院报销
     *
     * @param id 后台-小院报销主键
     * @return 后台-小院报销
     */
    public AdminXiaoYuanReimbursement selectAdminXiaoYuanReimbursementById(Long id) {
        return adminXiaoYuanReimbursementMapper.selectById(id);
    }

    /**
     * 查询后台-小院报销列表
     *
     * @param dto 后台-小院报销
     * @return 后台-小院报销
     */
    public TableDataInfo selectAdminXiaoYuanReimbursementList(AdminXiaoYuanReimbursement dto) {

        LambdaQueryWrapper<AdminXiaoYuanReimbursement> wrapper = new LambdaQueryWrapper<>();
        // 年份
        wrapper.eq(ObjectUtils.isNotEmpty(dto.getYears()), AdminXiaoYuanReimbursement::getYears, dto.getYears());
        // 项目名称的like查询
        wrapper.like(ObjectUtils.isNotEmpty(dto.getProjectName()), AdminXiaoYuanReimbursement::getProjectName, dto.getProjectName());

        wrapper.eq(ObjectUtils.isNotEmpty(dto.getIsActive()), AdminXiaoYuanReimbursement::getIsActive, dto.getIsActive());
        // 月份的范围查询
        if (ObjectUtils.isNotEmpty(dto.getMonthMin()) && ObjectUtils.isNotEmpty(dto.getMonthMax())) {
            // 月份只能再1-12中选择
            if ((dto.getMonthMin() > 12 || dto.getMonthMax() > 12) || (dto.getMonthMin() < 1 || dto.getMonthMax() < 1)) {
                throw new CustomException("月份只能再1-12中选择");
            }
            if (dto.getMonthMin() > dto.getMonthMax()) {
                throw new CustomException("开始月份不能大于结束月份");
            }
            wrapper.between(AdminXiaoYuanReimbursement::getMonth, dto.getMonthMin(), dto.getMonthMax());
        }
        // 单价范围
        if (ObjectUtils.isNotEmpty(dto.getUnitPriceMin()) && ObjectUtils.isNotEmpty(dto.getUnitPriceMax())) {
            if (dto.getUnitPriceMin().compareTo(dto.getUnitPriceMax()) > 0) {
                throw new CustomException("单价最小值不能超过单价最大值");
            }
            wrapper.between(AdminXiaoYuanReimbursement::getUnitPrice, dto.getUnitPriceMin(), dto.getUnitPriceMax());
        }
        // 总金额的范围查询
        if (ObjectUtils.isNotEmpty(dto.getTotalAmountMin()) && ObjectUtils.isNotEmpty(dto.getTotalAmountMax())) {
            if (dto.getTotalAmountMin().compareTo(dto.getTotalAmountMax()) > 0) {
                throw new CustomException("总金额最小值不能超过总金额最大值");
            }
            wrapper.between(AdminXiaoYuanReimbursement::getTotalAmount, dto.getTotalAmountMin(), dto.getTotalAmountMax());
        }

        wrapper.orderByDesc(AdminXiaoYuanReimbursement::getYears);
        wrapper.orderByDesc(AdminXiaoYuanReimbursement::getMonth);
        wrapper.orderByDesc(AdminXiaoYuanReimbursement::getId);
        Page<AdminXiaoYuanReimbursement> pageParam = MyPageUtils.handlePageParam();
        Page<AdminXiaoYuanReimbursement> adminPlotDBPage = adminXiaoYuanReimbursementMapper.selectPage(pageParam, wrapper);
        return TableDataInfo.covert(adminPlotDBPage);
    }

    /**
     * 新增后台-小院报销
     *
     * @param adminXiaoYuanReimbursement 后台-小院报销
     * @return 结果
     */
    public int insertAdminXiaoYuanReimbursement(AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        //
        Dict time = getTime();
        if (ObjectUtils.isEmpty(time)){
            throw new CustomException("请选择时间");
        }
        adminXiaoYuanReimbursement.setYears(time.getInt("years"));
        adminXiaoYuanReimbursement.setMonth(time.getInt("month"));

        return adminXiaoYuanReimbursementMapper.insert(adminXiaoYuanReimbursement);
    }

    /**
     * 修改后台-小院报销
     *
     * @param adminXiaoYuanReimbursement 后台-小院报销
     * @return 结果
     */
    public int updateAdminXiaoYuanReimbursement(AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        return adminXiaoYuanReimbursementMapper.updateById(adminXiaoYuanReimbursement);
    }

    public Dict getTime() {
        String key = RedisKeyConstants.XIAOYUAN_REIMBURSE_TIME;
        Object cacheObject = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(cacheObject)){
            return null;
        }
        String time = (String) cacheObject;
        String[] s = time.split(" ");
        Dict dict = Dict.create();
        dict.put("years", s[0]);
        dict.put("month", s[1]);
        return dict;
    }

    public void timeSave(AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        if (ObjectUtils.isEmpty(adminXiaoYuanReimbursement.getYears()) || ObjectUtils.isEmpty(adminXiaoYuanReimbursement.getMonth())){
            throw new CustomException("请选择时间");
        }
        String value = adminXiaoYuanReimbursement.getYears() + " " + adminXiaoYuanReimbursement.getMonth();
        redisCache.setCacheObject(RedisKeyConstants.XIAOYUAN_REIMBURSE_TIME, value, 3, TimeUnit.HOURS);
    }

    public void timeRemove() {
        redisCache.deleteObject(RedisKeyConstants.XIAOYUAN_REIMBURSE_TIME);
    }
}
